<?php

/**
 * Source code under Creative Commons License
 * (see LICENSE FILE)
 *
 * Copyright (c) 2013 Maelstrom.
 * All rights reserved.
 *
 *
 * @author      Maelstrom <devcreamframework@gmail.com>
 * @copyright   2012 Maelstrom.
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @link        https://github.com/maelstromkd/cream-dev.git
 * @version     1.2
 */


require realpath(dirname(__FILE__)) . '/../../core/paths.php';
require __CORE__ . 'core.php';

$_APP = new Backend();
$_APP->setCache(false);

switch($_GET['action']){

	case 'client':
		if($_APP->check('post','id')){
			$data = $_APP->_CRUD->read('civility,name,firstname FROM clients
				WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');
			$_APP->_CRUD->delete('clients WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');

			$return = $data[0]['civility'] . ' ' . $data[0]['firstname'] . ' ' . $data[0]['name'];
			unset($data);
			echo $return;
		}
	break;

	case 'newsletter':
		if($_APP->check('post','id')){
			$data = $_APP->_CRUD->read('subject FROM newsletters
				WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');
			$_APP->_CRUD->delete('newsletters WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');

			$return = $data[0]['subject'];
			unset($data);
			echo $return;
		}
	break;

	case 'mailinglist':
		if($_APP->check('post','id')){
			$data = $_APP->_CRUD->read('* FROM mailinglist
				WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');
			$_APP->_CRUD->delete('mailinglist WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');

			$return = $data[0]['email'];
			unset($data);
			echo $return;
		}
	break;

	case 'articles':
		if($_APP->check('post','id')){
			$data = $_APP->_CRUD->read('title FROM news
				WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');
			$_APP->_CRUD->delete('news WHERE id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');

			$return = $data[0]['title'];
			unset($data);
			echo $return;
		}
	break;

	case 'pages':
		if($_APP->check('post','id')){
			if($_APP->check('post','id')){
				$_APP->_PAGE->delete($_POST['id']);
				if(!empty($_APP->_PAGE->notification)){
					echo $_APP->_PAGE->notification;
				}
			}
		}
	break;

	case 'main':
		if($_APP->check('post','online')){
			file_put_contents(__CONTROL__, ($_POST['online'] == 2 ? 0 : 1));
		}
	break;

	case 'cache':
		if($_APP->check('post','online')){
			if(!$_APP->check('get','t')){
				$cache = parse_ini_file(__CACHE_CONFIG__,true);
				$newCache  = '[parameters]' . "\n";
				$newCache .= 'enabled = ' .  ($_POST['online'] == 2 ? 0 : 1) . "\n";
				$newCache .= 'expire = ' . $cache['parameters']['expire'] . "\n";
				file_put_contents(__CACHE_CONFIG__, $newCache);
			}else{
				if($_GET['t'] == 'expire'){
					$cache = parse_ini_file(__CACHE_CONFIG__,true);
					$newCache  = '[parameters]' . "\n";
					$newCache .= 'enabled = ' .  $cache['parameters']['enabled'] . "\n";
					$newCache .= 'expire = ' . $_POST['online'] . "\n";
					file_put_contents(__CACHE_CONFIG__, $newCache);
				}
			}
		}
	break;

	case 'category':
		$data = $_APP->_CRUD->read('name from categories
			where id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');
		$return = $data[0]['name'];
		$_APP->_CRUD->delete('categories where id=\'' . mysql_real_escape_string((int)$_POST['id']) . '\'');
		$_APP->_CATEGORIES->rebuild();
		echo $return;
	break;
}
